﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.ComponentModel;
using System.Collections;
using System.Drawing.Design;

[assembly: WebResource("Water.Resources.Default.JS.TabControl.TabControl.js", "text/javascript")]
namespace Water.Web.Controls
{
    //[ParseChildren(typeof(TabPage))]
    //[ControlBuilder(typeof(TabBuilder))]
    [ParseChildren(true)]
    [Designer(typeof(ContainerControlDesigner))]
    [ToolboxData("<{0}:TabControl runat=server></{0}:TabControl>")]
    public class TabControl : DataBoundControl
    {

        private TextAlign tabsAlign = TextAlign.Left;

        [Browsable(true), GlobalizeDescription("TabsAlign"), Category("Appearance"), Bindable(true), DefaultValue(TextAlign.Left)]
        public TextAlign TabsAlign
        {
            get { return tabsAlign; }
            set { tabsAlign = value; }
        }

        private string titleText;

        [Browsable(true), GlobalizeDescription("TitleText"), Category("Appearance"), Bindable(true)]
        public virtual string TitleText
        {
            get { return titleText; }
            set { titleText = value; }
        }

        private string titleImageUrl = "~/Resources/Default/Images/Layout/info.gif";

        [Browsable(true), GlobalizeDescription("TitleImageUrl"), Category("Appearance"), Bindable(true), DefaultValue("~/Resources/Default/Images/Layout/info.gif"), Editor("System.Web.UI.Design.ImageUrlEditor", typeof(UITypeEditor))]
        public virtual string TitleImageUrl
        {
            get { return titleImageUrl; }
            set { titleImageUrl = value; }
        }

        private TabPageItemCollection tabs;

        [Browsable(true), Category("Data"), GlobalizeDescription("Items"), NotifyParentProperty(true), TypeConverter(typeof(CollectionConverter))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public TabPageItemCollection Tabs
        {
            get
            {
                if (this.tabs == null)
                    this.tabs = new TabPageItemCollection();
                return this.tabs;
            }
            set { tabs = value; }
        }


        //data fields
        private string titleField;

        [Browsable(true), GlobalizeDescription("TitleField"), Category("Data")]
        public string TitleField
        {
            get { return titleField; }
            set { titleField = value; }
        }
        private string selectedField;

        [Browsable(true), GlobalizeDescription("SelectedField"), Category("Data")]
        public string SelectedField
        {
            get { return selectedField; }
            set { selectedField = value; }
        }
        private string titleImageUrlField;

        [Browsable(true), GlobalizeDescription("TitleImageUrlField"), Category("Data")]
        public string TitleImageUrlField
        {
            get { return titleImageUrlField; }
            set { titleImageUrlField = value; }
        }
        private string navigateUrlField;

        [Browsable(true), GlobalizeDescription("NavigateUrlField"), Category("Data")]
        public string NavigateUrlField
        {
            get { return navigateUrlField; }
            set { navigateUrlField = value; }
        }
        private string allowTargetInBlankField;

        [Browsable(true), GlobalizeDescription("AllowTargetInBlankField"), Category("Data")]
        public string AllowTargetInBlankField
        {
            get { return allowTargetInBlankField; }
            set { allowTargetInBlankField = value; }
        }
        private string navigateTypeField;

        [Browsable(true), GlobalizeDescription("NavigateTypeField"), Category("Data")]
        public string NavigateTypeField
        {
            get { return navigateTypeField; }
            set { navigateTypeField = value; }
        }

        private string heightField;

        [Browsable(true), GlobalizeDescription("HeightField"), Category("Data")]
        public string HeightField
        {
            get { return heightField; }
            set { heightField = value; }
        }

        private ITemplate headerTemplate;

        [Browsable(false)]
        [TemplateContainer(typeof(TabsHeaderTemplatecontainer))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate HeaderTemplate
        {
            get { return headerTemplate; }
            set { headerTemplate = value; }
        }


        protected override void OnLoad(EventArgs e)
        {
            PageManager.Register(this);
            base.OnLoad(e);
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabs");
        }

        /*
        protected override void AddParsedSubObject(object obj)
        {
            if (obj is TabPage)
                base.AddParsedSubObject(obj);
        }
        */


        private TabsHeaderTemplatecontainer tabsHeaderContainer;
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (this.HeaderTemplate != null && tabsHeaderContainer == null)
            {
                tabsHeaderContainer = new TabsHeaderTemplatecontainer();
                this.HeaderTemplate.InstantiateIn(tabsHeaderContainer);
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            this.Page.ClientScript.RegisterClientScriptResource(typeof(TabControl), "Water.Resources.Default.JS.TabControl.TabControl.js");
            PageManager.RegisterPageStyleSheet(this, "TabControl_CSS", "CSS/TabControl.css");
            base.OnPreRender(e);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            ArrayList contains = new ArrayList();

            //header
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_header");
            if (TextAlign.Left == this.TabsAlign)
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabs_header_l");
            else if (TextAlign.Right == this.TabsAlign)
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabs_header_r");

            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            //tab begin
            for (int i = 0; i < this.tabs.Count; i++)
            {
                TabPage tab = (this.Tabs[i]);
                tab.Index = i;

                string prefix = tab.Selected ? "tab_s_" : "tab_";

                //left
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_tab_l_" + i);
                if (i == 0)
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, prefix + "l0");
                else
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, prefix + "l");
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.RenderEndTag();

                //middle
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_tab_m_" + i);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, prefix + "m");
                writer.RenderBeginTag(HtmlTextWriterTag.Span);

                //tab table
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_tab_table_" + i);
                writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
                writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "tab_table");
                writer.RenderBeginTag(HtmlTextWriterTag.Table);

                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                if (!string.IsNullOrEmpty(tab.TitleImageUrl))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "tab_title_img");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);   //title image td
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, this.ResolveClientUrl(tab.TitleImageUrl));
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                    writer.RenderEndTag();//title image td end 
                }
                writer.RenderBeginTag(HtmlTextWriterTag.Td);

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "tab_a");
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_a" + i);
                if (!string.IsNullOrEmpty(tab.NavigateUrl))
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, tab.NavigateUrl);

                if (TabNavigateType.Contain == tab.NavigateType)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "doTab(" + i + ",'" + this.ClientID + "',0);return false;");
                    contains.Add(i);
                }
                else if (TabNavigateType.Ajax == tab.NavigateType)
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "doTab(" + i + ",'" + this.ClientID + "',1);return false;");
                else if (TabNavigateType.Iframe == tab.NavigateType)
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "doTab(" + i + ",'" + this.ClientID + "',2);return false;");

                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.Write(tab.Title);
                writer.RenderEndTag();//A

                writer.RenderEndTag();//td

                if (!string.IsNullOrEmpty(tab.NavigateUrl) && tab.AllowTargetInBlank)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "tab_blank_img");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);   //tab blank td

                    //open in new window
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, tab.NavigateUrl);
                    writer.AddAttribute(HtmlTextWriterAttribute.Target, "_blank");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);

                    writer.AddAttribute(HtmlTextWriterAttribute.Src, this.ResolveClientUrl("~/Resources/Default/Images/TabControl/open.gif"));
                    writer.AddAttribute(HtmlTextWriterAttribute.Title, Water.Resources.Globalize.MessageResource.OpenTargetInBlank);
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();

                    writer.RenderEndTag();
                    //open in new window

                    writer.RenderEndTag();//tab blank td
                }
                if (!string.IsNullOrEmpty(tab.NavigateUrl))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "tab_refresh_img");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);   //tab blank td

                    //refresh
                    if (TabNavigateType.Ajax == tab.NavigateType)
                        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "doTab(" + i + ",'" + this.ClientID + "',1,true);return false;");
                    else if (TabNavigateType.Iframe == tab.NavigateType)
                        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "doTab(" + i + ",'" + this.ClientID + "',2,true);return false;");
                    writer.AddAttribute(HtmlTextWriterAttribute.Target, "_blank");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);

                    writer.AddAttribute(HtmlTextWriterAttribute.Src, this.ResolveClientUrl("~/Resources/Default/Images/refresh.gif"));
                    writer.AddAttribute(HtmlTextWriterAttribute.Title, Water.Resources.Globalize.MessageResource.Refresh);
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();

                    writer.RenderEndTag();
                    //refresh

                    writer.RenderEndTag();//tab blank td
                }

                writer.RenderEndTag();//tr

                writer.RenderEndTag();//tab table


                writer.RenderEndTag();//middle

                //right
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_tab_r_" + i);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, prefix + "r");
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.RenderEndTag();
            }
            //tab end

            writer.RenderEndTag();//header end

            //header line
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabs_header_line");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            RenderTitleBar(writer);
            writer.RenderEndTag();//header end


            //container
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabs_contain");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            //foreach (Control c in this.Controls)
            //{
            //    c.RenderControl(writer);
            //}
            foreach (TabPage t in this.Tabs)
            {
                this.Controls.Add(t);
                t.RenderControl(writer);
            }

            writer.RenderEndTag();
        }

        private void RenderTitleBar(HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(this.TitleImageUrl) || !string.IsNullOrEmpty(TitleText))
            {
                //是否显示TitleImage
                if (!string.IsNullOrEmpty(this.TitleImageUrl))
                {
                    if (TextAlign.Left == this.TabsAlign)
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabs_title_r tabs_title_img");
                    else if (TextAlign.Right == this.TabsAlign)
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabs_title_l tabs_title_img");

                    writer.RenderBeginTag(HtmlTextWriterTag.Span);   //title image td
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, this.ResolveClientUrl(this.TitleImageUrl));
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                    writer.RenderEndTag();//title image td end 
                }

                //是否显示TitleText
                if (!string.IsNullOrEmpty(TitleText))
                {
                    if (TextAlign.Left == this.TabsAlign)
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabs_title_r tabs_title");
                    else if (TextAlign.Right == this.TabsAlign)
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabs_title_l tabs_title");

                    writer.RenderBeginTag(HtmlTextWriterTag.Span);
                    writer.Write(TitleText);
                    writer.RenderEndTag();
                }
                if (this.tabsHeaderContainer != null)
                {
                    this.Controls.Add(this.tabsHeaderContainer);
                    this.tabsHeaderContainer.RenderControl(writer);
                }

            }
        }

        protected override void PerformDataBinding(IEnumerable data)
        {
            base.PerformDataBinding(data);
            if (data != null)
            {
                foreach (object obj in data)
                {
                    TabPage tab = new TabPage();
                    if (!string.IsNullOrEmpty(this.TitleField))
                        tab.Title = DataBinder.GetPropertyValue(obj, this.TitleField, null);
                    if (!string.IsNullOrEmpty(this.TitleImageUrlField))
                        tab.TitleImageUrl = DataBinder.GetPropertyValue(obj, this.TitleImageUrlField, null);
                    if (!string.IsNullOrEmpty(this.NavigateUrlField))
                        tab.NavigateUrl = DataBinder.GetPropertyValue(obj, this.NavigateUrlField, null);
                    if (!string.IsNullOrEmpty(this.selectedField))
                    {
                        bool selected = false;
                        string str_selected = DataBinder.GetPropertyValue(obj, this.selectedField, null);
                        if (str_selected == "1")
                            selected = true;
                        tab.Selected = selected;

                    }
                    if (!string.IsNullOrEmpty(this.AllowTargetInBlankField))
                    {
                        bool allowTargetInBlank = false;
                        string str_allowTargetInBlank = DataBinder.GetPropertyValue(obj, this.AllowTargetInBlankField, null);
                        if (str_allowTargetInBlank == "1")
                            allowTargetInBlank = true;
                        tab.AllowTargetInBlank = allowTargetInBlank;

                    }
                    if (!string.IsNullOrEmpty(this.NavigateTypeField))
                    {
                        string str_navigateTpe = DataBinder.GetPropertyValue(obj, this.NavigateTypeField, null);
                        switch (str_navigateTpe)
                        {
                            case "0":
                                tab.NavigateType = TabNavigateType.Contain;
                                break;
                            case "1":
                                tab.NavigateType = TabNavigateType.Iframe;
                                break;
                            case "2":
                                tab.NavigateType = TabNavigateType.Ajax;
                                break;
                            default:
                                tab.NavigateType = TabNavigateType.Iframe;
                                break;
                        }
                    }
                    if (!string.IsNullOrEmpty(this.HeightField))
                    {
                        string str_height = DataBinder.GetPropertyValue(obj, this.HeightField, null);
                        if (!string.IsNullOrEmpty(str_height))
                        {
                            int height = Int32.Parse(str_height);
                            tab.Height = Unit.Pixel(height);
                        }
                    }
                    this.Tabs.Add(tab);

                }
            }
        }


    }
    [ToolboxItem(false)]
    public class TabsHeaderTemplatecontainer : WebControl, INamingContainer
    {
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            TabControl tc = (TabControl)this.Parent;
            if (null != tc)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, tc.ClientID + "_header_t");
                if (tc.TabsAlign == TextAlign.Right)
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabs_header_t_r");
                else
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabs_header_t_l");
            }
            base.AddAttributesToRender(writer);
        }
    }
}
